package com.atlassian.tutorial.listener;


import com.atlassian.confluence.event.events.security.LoginEvent;
import com.atlassian.confluence.event.events.security.LogoutEvent;
import com.atlassian.event.Event;
import com.atlassian.event.EventListener;
import org.apache.log4j.Logger;

/**
 * @author gaowl@stu.jiangnan.edu.cn
 * @version 2021/07/02/15:39
 */
public class UserLoginListener implements EventListener {
    private static final Logger log = Logger.getLogger(UserLoginListener.class);

    private Class[] handledClasses = new Class[]{ LoginEvent.class, LogoutEvent.class};

    @Override
    public void handleEvent(Event event) {
        if (event instanceof LoginEvent){
            LoginEvent loginEvent = (LoginEvent) event;
            log.info("====================" + loginEvent.getUsername() + " logged in (" +  loginEvent.getSessionId() + ")");
        }else if (event instanceof LogoutEvent){
            LogoutEvent logoutEvent = (LogoutEvent) event;
            log.info("====================" + logoutEvent.getUsername() + " logged out (" +  logoutEvent.getSessionId() + ")");
        }
    }

    @Override
    public Class[] getHandledEventClasses() {
        return handledClasses;
    }
}
